package com.ansion.baseui.base.toolbar;

import android.view.Gravity;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

import com.ansion.basetool.preconditions.Preconditions;
import com.ansion.baseui.base.actionbar.TActionBar;
import com.ansion.utils.ResourceUtils;
import com.ansion.utils.UiUtils;

import java.util.List;


/**
 * created by zhaoyuntao
 * on 02/08/2022
 * description:
 */
public interface TActionBarSupport {
    TActionBar getTActionBar();

    default void setMenus(@NonNull List<TMenuItem> menus) {
        Preconditions.checkNotNull(menus);
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.post(() -> {
                tActionBar.setMenus(menus);
            });
        }
    }

    default void setMenus(@NonNull TMenuItem... menus) {
        Preconditions.checkNotNull(menus);
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.post(() -> {
                tActionBar.setMenus(menus);
            });
        }
    }

    default void clearMenus() {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.clearMenu();
        }
    }

    @NonNull
    default View setCustomToolbar(@LayoutRes int layoutRes) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            return tActionBar.setCustomTitleView(layoutRes);
        } else {
            throw new RuntimeException("You have to set actionBar before calling setCustomToolbar");
        }
    }

    @NonNull
    default View setCustomToolbar(@NonNull View customToolbar) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            return tActionBar.setCustomTitleView(customToolbar);
        } else {
            throw new RuntimeException("You have to set actionBar before calling setCustomToolbar");
        }
    }

    default boolean onSetTActionBarVisible() {
        return true;
    }

    default boolean onSetTActionBarImmersion() {
        return false;
    }

    default int onInitTActionBarHeight() {
        return UiUtils.dipToPx(48);
    }

    default void setToolbarArrowViewVisible(boolean visible) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setArrowViewVisible(visible);
        }
    }

    default boolean isTActionBarVisible() {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            return tActionBar.getVisibility() == View.VISIBLE;
        } else {
            return false;
        }
    }

    default int getTActionBarHeight() {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            return tActionBar.getMeasuredHeight();
        } else {
            return 0;
        }
    }

    default void setTActionBarAlpha(float alpha) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setAlpha(alpha);
        }
    }

    default void setTActionBarVisible(boolean visible) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setVisibility(visible ? View.VISIBLE : View.GONE);
        }
    }

    default void setTitle(int titleId) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setTitle(ResourceUtils.getString(titleId));
        }
    }

    default void setTitle(CharSequence title) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setTitle(title);
        }
    }

    default void setSubtitle(CharSequence subtitle) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setSubtitle(subtitle);
        }
    }

    default void setSubtitle(@StringRes int subtitleStringRes) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setSubtitle(ResourceUtils.getString(subtitleStringRes));
        }
    }

    default void setTitle(CharSequence title, int gravity) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setTitle(title, gravity);
        }
    }

    default void setSubtitle(CharSequence subtitle, int gravity) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setSubtitle(subtitle, gravity);
        }
    }

    default void setSubtitleColor(@ColorInt int textColor) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setSubTitleColor(textColor);
        }
    }

    default void setBarTitleColor(@ColorInt int textColor) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setTitleColor(textColor);
        }
    }

    default void setCenterTitle(@StringRes int titleId) {
        setCenterTitle(ResourceUtils.getString(titleId));
    }

    default void setCenterTitle(String title) {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.setTitle(title, Gravity.CENTER);
        }
    }

    default void closePopWindow() {
        TActionBar tActionBar = getTActionBar();
        if (tActionBar != null) {
            tActionBar.closePopWindow();
        }
    }
}
